# -*- coding: utf-8 -*-
# @Time    : 2021/4/10 20:30
# @Author  : Suwenyang
# @Email   : 2242142766@qq.com
# @File    : 爬虫+正则.py

# 爬取 https://www.sanchuangedu.cn网站上的图片下载下来保存到本地
import requests
import re
url = "https://www.sanchuangedu.cn"
ret = requests.get(url)
pic_list = re.findall(r"(?:src=).*(?:.*.jpg)|(?:src=).*(?:.*.png)", ret.text)
i = 1
for key in pic_list:
    key = url+key[5:]
    print("开始下载图片："+key+"\r\n")
    try:
        pic = requests.get(key,timeout=10)
    except requests.exceptions.ConnectionError:
        print("图片无法下载")
        continue
    # 保存图片路径
    dir = "D:\project\py_code\su\正则表达式\作业\sanchuang_pic" + str(i) + ".jpg"
    fp = open(dir, 'wb')
    fp.write(pic.content)
    fp.close()
    i += 1